import { link } from '../../vant-weapp/dist/mixins/link'
import { VantComponent } from '../../vant-weapp/dist/common/component'
VantComponent({
  mixins: [link],
  props: {
    customClass: String,
    ratio: {
      type: Number,
      value: 0.75
    },
    color: String,
    shadowLines: {
      type: Number,
      value: 4
    },
    radius: null,
    memberCount: null,
    memberCountText: String,
    title: String,
    icon: String,
    label: String,
    labelText: String,
    date: String,
    dateText: String,
    useEmptySlot: Boolean,
    border: {
      type: Boolean,
      value: true
    },
    showBadge: {
      type: Boolean,
      value: false
    },
    showActionTitle: {
      type: Boolean,
      value: false
    },
    members: {
      type: Array,
      value: () => {
        return []
      }
    },
    actions: {
      type: Array,
      value: []
      // observer: 'formatActionItemList'
    },
    actionTitle: String,
    actionIcon: String,
    cover: String
  },
  data: {
    actionText: []
  },
  methods: {
    // 更新子项列表
    formatActionItemList () {
      const { actions } = this.data
      if (actions && actions.length > 0) {
        this.setData({
          actionText: actions.map((item) => {
            return item.name
          })
        })
      }
    },
    onShowAction () {
      console.group('onShowAction')
      const { actions, showActionTitle, actionTitle, title } = this.data
      if (actions && actions.length > 0) {
        wx.showActionSheet({
          alertText: showActionTitle && (actionTitle || title),
          itemList: actions.map((item) => {
            return item.name
          }),
          success: (res) => {
            this.onActionSheetClick(res.tapIndex)
          }
        })
      }
      console.groupEnd('onShowAction')
    },
    onActionSheetClick (index) {
      const { actions } = this.data
      const item = actions[index] || {}
      if (!item) {
        return
      }
      this.$emit('action', item)
    },
    onClick (event) {
      this.$emit('click', event.detail)
      this.jumpLink()
    }
  }
})
